package com.chushouya.manager.service.common.impl;

import com.alipay.api.domain.*;
import com.alipay.api.response.*;
import com.chushouya.common.applet.AlipayClient;
import com.chushouya.common.support.DevOption;
import com.chushouya.manager.service.common.AlipayClientCommonService;
import com.chushouya.manager.service.common.AlipayClientFactory;
import com.general.framework.core.exception.Ex;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Jsons;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class AlipayClientCommonServiceImpl implements AlipayClientCommonService {


    @Value("${com.alipay.appId:2016102400751234}")
    private String appId;

    @Value("${com.alipay.minAppId}")
    private String minAppId;


    @Override
    public void alipayFundWalletTemplateCreate(String orderNo) {
        AlipayClient client = AlipayClientFactory.getInstance(appId);
        client.alipayFundWalletTemplateCreate(orderNo);
    }

    @Override
    public void alipayFundWalletTemplateConfirm() {
        AlipayClientFactory.getInstance(appId).alipayFundWalletTemplateConfirm();
    }

    @Override
    public void closeFundWallet(String userWalletId) {
        AlipayClientFactory.getInstance(appId).alipayFundWalletDestroy(userWalletId);
    }

    @Override
    public AlipayFundWalletRefundResponse refundFundWallet(String outBizNo, BigDecimal amount, String userWalletId) {
        return AlipayClientFactory.getInstance(appId).alipayFundWalletRefund(outBizNo, amount.toString(), userWalletId);
    }

    @Override
    public AlipayFundWalletWithdrawResponse withdrawFundWallet(String outBizNo, BigDecimal amount, String userWalletId) {
        return AlipayClientFactory.getInstance(appId).alipayFundWalletWithdraw(outBizNo, amount.toString(), userWalletId);
    }

    @Override
    public AlipayFundWalletConsultResponse queryFundWallet(String userWalletId) {
        return AlipayClientFactory.getInstance(appId).alipayFundWalletConsult(userWalletId);
    }

    @Override
    public AlipayFundWalletTokenCreateResponse createWalletOpenUrl(String outBizNo,String openId) {
        final String walletTemplateId = "2025052866206001180000004084"; // 需要替换为实际的支付宝钱包模板ID
        return AlipayClientFactory.getInstance(appId).alipayFundWalletTokenCreate(outBizNo, walletTemplateId, openId);
    }

    @Override
    public String alipayFundWalletDepositOrderCreate(String outBizNo, String amount, String userWalletId) {
        AlipayClient client = AlipayClientFactory.getInstance(appId);
        return client.alipayFundWalletDepositOrderCreate(outBizNo, amount, userWalletId);
    }

    @Override
    public String alipayFundTransAppPay(String outBizNo, String amount, String orderId) {
        AlipayClient client = AlipayClientFactory.getInstance(appId);
        return client.alipayFundTransAppPay(outBizNo, amount, orderId);
    }

    /**
     * 支付宝转账
     *
     * @return
     */
    @Override
    public AlipayFundTransUniTransferResponse fundTransUniTransfer(String outBizNo, String transAmount, String orderTitle, String remark, String identity, String identityType, String name) {
        log.info("==========================>支付宝转账APPID：{}", appId);
        if(DevOption.isDevModel()){
            if (!appId.startsWith("9021000")) {
                if (new BigDecimal(transAmount).compareTo(new BigDecimal("10")) > 0) {
                    throw Ex.business("测试环境转账金额不能大于10元");
                }
            }
        }
        AlipayClient client = AlipayClientFactory.getInstance(appId);
        AlipayFundTransUniTransferResponse response =
                client.fundTransUniTransfer(outBizNo, transAmount, orderTitle, remark, identity, identityType, name);
        return Beans.copy(response, AlipayFundTransUniTransferResponse.class);
    }

    @Override
    public String alipayTradeWapPay(String appId, String orderNo, BigDecimal payPrice, String subject, String notifyUrl, String returnUrl) {
        AlipayClient client = AlipayClientFactory.getInstance(appId);
        return client.alipayTradeWapPay(orderNo, payPrice.toString(), subject, notifyUrl, returnUrl);
    }

    @Override
    public AlipayOpenAppQrcodeCreateResponse alipayOpenAppQrcodeCreate(String urlParam, String queryParam, String size, String describe) {
        return AlipayClientFactory.getInstance(appId).alipayOpenAppQrcodeCreate(urlParam, queryParam,  size, describe);
    }

    @Override
    public AlipayTradePayResponse walletAlipayTradePay(String smId,String outTradeNo, BigDecimal amount, String agreementNo) {
        return AlipayClientFactory.getInstance(appId).walletAlipayTradePay(smId,outTradeNo, amount, agreementNo);
    }


    @Override
    public String alipayTradeCreate(String orderNo,String openId, String totalAmount, String subject, List<GoodsDetail> goodsList) {
        AlipayClient client = AlipayClientFactory.getInstance(appId);
        return client.alipayTradeCreate(appId, openId, orderNo, totalAmount, subject, goodsList);
    }

    @Override
    public String alipayOpenFileUpload(String image) {
        return AlipayClientFactory.getInstance(appId).alipayOpenFileUpload(image);
    }

    @Override
    public void alipayCommerceRecycleQcreportUpload(AlipayCommerceRecycleQcreportUploadModel model) {
        AlipayClientFactory.getInstance(appId).alipayCommerceRecycleQcreportUpload(model);
    }

    @Override
    public AlipayOpenMiniOrderCreateResponse alipayOpenMiniOrderCreate(AlipayOpenMiniOrderCreateModel model) {
        return AlipayClientFactory.getInstance(minAppId).alipayOpenMiniOrderCreate(model);
    }

    @Override
    public void alipayOpenMiniOrderDeliverySend(AlipayOpenMiniOrderDeliverySendModel model) {
        AlipayClientFactory.getInstance(minAppId).alipayOpenMiniOrderDeliverySend(model);
    }

    @Override
    public void alipayOpenMiniOrderDeliveryReceive(AlipayOpenMiniOrderDeliveryReceiveModel model) {
        AlipayClientFactory.getInstance(minAppId).alipayOpenMiniOrderDeliveryReceive(model);
    }

    @Override
    public AlipayOpenMiniOrderDeliveryModifyResponse alipayOpenMiniOrderDeliveryModify(AlipayOpenMiniOrderDeliveryModifyModel model) {
        return AlipayClientFactory.getInstance(minAppId).alipayOpenMiniOrderDeliveryModify(model);
    }

    @Override
    public void alipayOpenMiniOrderAftersaleSync(AlipayOpenMiniOrderAftersaleSyncModel model) {
        AlipayClientFactory.getInstance(minAppId).alipayOpenMiniOrderAftersaleSync(model);
    }

    @Override
    public void alipayOpenMiniOrderClose(AlipayOpenMiniOrderCloseModel alipayOpenMiniOrderCloseModel) {
        AlipayClientFactory.getInstance(minAppId).alipayOpenMiniOrderClose(alipayOpenMiniOrderCloseModel);
    }

    @Override
    public AlipayOpenMiniOrderQueryResponse alipayOpenMiniOrderQuery(String orderNo, String openId) {
        AlipayOpenMiniOrderQueryModel model = new AlipayOpenMiniOrderQueryModel();
        model.setOutOrderId(orderNo);
        model.setUserId(openId);
        return AlipayClientFactory.getInstance(minAppId).alipayOpenMiniOrderQuery(model);
    }

    @Override
    public AntMerchantExpandShopCreateResponse antMerchantExpandShopCreate(AntMerchantExpandShopCreateModel model) {
        log.debug("芝麻门店创建返回: {}", Jsons.toFormatJsonString(model));
        return AlipayClientFactory.getInstance(appId).antMerchantExpandShopCreate(model);
    }

    @Override
    public AntMerchantExpandShopModifyResponse antMerchantExpandShopModify(AntMerchantExpandShopModifyModel model) {
        log.debug("芝麻门店修改返回: {}", Jsons.toFormatJsonString(model));
        return AlipayClientFactory.getInstance(appId).antMerchantExpandShopModify(model);
    }

    @Override
    public AlipayCommerceRecycleDeductRelationSaveResponse alipayCommerceRecycleDeductRelationSave(AlipayCommerceRecycleDeductRelationSaveModel model) {

        return AlipayClientFactory.getInstance(appId).alipayCommerceRecycleDeductRelationSave(model);
    }

    @Override
    public AlipayCommerceRecycleRoyaltyRelationSaveResponse alipayCommerceRecycleRoyaltyRelationSave(AlipayCommerceRecycleRoyaltyRelationSaveModel model) {

        return AlipayClientFactory.getInstance(appId).alipayCommerceRecycleRoyaltyRelationSave(model);
    }

    @Override
    public AlipayCommerceRecycleDeductRelationQueryResponse alipayCommerceRecycleDeductRelationQuery(AlipayCommerceRecycleDeductRelationQueryModel model) {
        return AlipayClientFactory.getInstance(appId).alipayCommerceRecycleDeductRelationQuery(model);

    }

    @Override
    public AlipayCommerceRecycleRoyaltyRelationQueryResponse alipayCommerceRecycleRoyaltyRelationQuery(AlipayCommerceRecycleRoyaltyRelationQueryModel model) {
        return AlipayClientFactory.getInstance(appId).alipayCommerceRecycleRoyaltyRelationQuery(model);
    }

    @Override
    public AlipayFundWalletConsultResponse userWalletConsult(String userWalletId) {
        return AlipayClientFactory.getInstance(appId).alipayFundWalletConsult(userWalletId);
    }

    @Override
    public AlipayCommerceRecycleOrderPreCheckResponse alipayCommerceRecycleOrderPreCheck(AlipayCommerceRecycleOrderPreCheckModel model) {
        return AlipayClientFactory.getInstance(appId).alipayCommerceRecycleOrderPreCheck(model);
    }

    @Override
    public void alipayCommerceRecycleOrderMerchantConfirm(AlipayCommerceRecycleOrderMerchantConfirmModel model) {
        AlipayClientFactory.getInstance(appId).alipayCommerceRecycleOrderMerchantConfirm(model);
    }

    @Override
    public AlipayCommerceRecycleInstshopQrcodeConsultResponse alipayCommerceRecycleInstshopQrcodeConsult(String alipayShopId) {
        return AlipayClientFactory.getInstance(appId).alipayCommerceRecycleInstshopQrcodeConsult(alipayShopId);
    }

    @Override
    public void alipayFundWalletWithdraw(String outBizNo, String amount, String userWalletId) {
        AlipayClientFactory.getInstance(appId).alipayFundWalletWithdraw(outBizNo, amount, userWalletId);
    }

    @Override
    public AlipayOpenAppMiniTemplatemessageSendResponse sendMiniTemplateMessage(String toUserId, String userTemplateId, String page, String data) {
        AlipayClient client = AlipayClientFactory.getInstance(minAppId);
        AlipayOpenAppMiniTemplatemessageSendModel model = new AlipayOpenAppMiniTemplatemessageSendModel();
        model.setData(data);
        model.setPage(page);
        model.setToUserId(toUserId);
        model.setUserTemplateId(userTemplateId);
        return client.alipayOpenAppMiniTemplateMessageSend(model);
    }


}
